

//mapData数据结构
export interface MapdataType {
    name: string;
    value: [number, number, number]; //x,y,value  第一个x 第二个y  第三个value
}

// 业务分类数据结构
export interface CategoryDataType {
    name: string;
    category: string; // 分类：绿电供应、共享换电、共享出行、共享运力
    value: [number, number]; // 经度，纬度
    visible: boolean; // 是否显示
}

// 业务分类配置
export const BUSINESS_CATEGORIES = {
    SHARED_TRAVEL: { key: 'shared_travel', label: '共享出行', color: '#faad14', symbol: 'triangle', icon: 'http://image.shengxuankj.com/fronts/icon_06.png' },
} as const;
export const optionHandle = (regionCode: string, list: object[], mapData: MapdataType[], categoryData: CategoryDataType[] = []) => {
    let top = 45;
    let zoom = ["china"].includes(regionCode) ? 1.05 : 1;
    return {
        backgroundColor: "rgba(0,0,0,0)",
        tooltip: {
            show: false,
        },
        // 业务分类图例控制
        legend: {
            show: true,
            data: [
                {
                    name: BUSINESS_CATEGORIES.SHARED_TRAVEL.label,
                    icon: 'image://' + BUSINESS_CATEGORIES.SHARED_TRAVEL.icon,
                    textStyle: { color: '#fff' }
                }
            ],
            orient: 'vertical',
            left: 20,
            bottom: 20,
            backgroundColor: 'rgba(0, 0, 0, 0.6)',
            borderColor: 'rgba(124, 189, 207, 0.8)',
            borderWidth: 1,
            borderRadius: 5,
            padding: [10, 15],
            // 设置图例图标尺寸
            itemWidth: 20,
            itemHeight: 20,
            // 图标与文字间距
            itemGap: 10,
            textStyle: {
                color: '#fff',
                fontSize: 12
            },
            // 支持点击控制显示隐藏
            selectedMode: 'multiple'
        },
        visualMap: {
            show: false // 隐藏原有的visualMap，改用legend控制
        },
        geo: {
            map: regionCode,
            roam: false,
            selectedMode: false, //是否允许选中多个区域
            zoom: zoom,
            top: top,
            // aspectScale: 0.78,
            show: false,
        },
        series: [
            {
                name: "MAP",
                type: "map",
                map: regionCode,
                // aspectScale: 0.78,
                data: list,
                // data: [1,100],
                selectedMode: false, //是否允许选中多个区域
                zoom: zoom,
                geoIndex: 1,
                top: top,
                tooltip: {
                    show: true,
                    formatter: function (params: any) {
                        return '中国' + params.name;
                    },
                    backgroundColor: "rgba(0,0,0,.6)",
                    borderColor: "rgba(147, 235, 248, .8)",
                    textStyle: {
                        color: "#FFF",
                    },
                },
                label: {
                    show: false,
                    color: "#000",
                    // position: [-10, 0],
                    formatter: function (val: any) {
                        // console.log(val)
                        if (val.data !== undefined) {
                            return val.name.slice(0, 2);
                        } else {
                            return "";
                        }
                    },
                    rich: {},
                },
                emphasis: {
                    label: {
                        show: false,
                    },
                    itemStyle: {
                        // areaColor: "rgba(56,155,183,.7)",
                        areaColor:{
                            type: "radial",
                            x: 0.5,
                            y: 0.5,
                            r: 0.8,
                            colorStops: [
                                {
                                    offset: 0,
                                    color: "rgba(147, 235, 248, 0)", // 0% 处的颜色
                                },
                                {
                                    offset: 1,
                                    color: "rgba(56,155,183, .8)", // 100% 处的颜色
                                },
                            ],
                            globalCoord: false, // 缺为 false
                        },
                        borderWidth: 1,
                    },
                },
                itemStyle: {
                    borderColor: "rgba(147, 235, 248, .8)",
                    borderWidth: 1,
                    areaColor: {
                        type: "radial",
                        x: 0.5,
                        y: 0.5,
                        r: 0.8,
                        colorStops: [
                            {
                                offset: 0,
                                color: "rgba(147, 235, 248, 0)", // 0% 处的颜色
                            },
                            {
                                offset: 1,
                                color: "rgba(147, 235, 248, .2)", // 100% 处的颜色
                            },
                        ],
                        globalCoord: false, // 缺为 false
                    },
                    shadowColor: "rgba(128, 217, 248, .3)",
                    shadowOffsetX: -2,
                    shadowOffsetY: 2,
                    shadowBlur: 10,
                },
            },
            {
                data: mapData,
                type: "effectScatter",
                coordinateSystem: "geo",
                symbolSize: function (val: any) {
                    return 4;
                    // return val[2] / 50;
                },
                legendHoverLink: true,
                showEffectOn: "render",
                rippleEffect: {
                    // period: 4,
                    scale: 6,
                    color: "rgba(255,255,255, 1)",
                    brushType: "fill",
                },
                tooltip: {
                    show: true,
                    formatter: function (params: any) {
                        if (params.data) {
                            return '中国' + params.name + "：" + '1980';
                        } else {
                            return '中国' + params.name;
                        }
                    },
                    backgroundColor: "rgba(0,0,0,.6)",
                    borderColor: "rgba(147, 235, 248, .8)",
                    textStyle: {
                        color: "#FFF",
                    },
                },
                label: {
                    formatter: (param: any) => {
                        return param.name.slice(0, 2);
                    },

                    fontSize: 11,
                    offset: [0, 2],
                    position: "bottom",
                    textBorderColor: "#fff",
                    textShadowColor: "#000",
                    textShadowBlur: 10,
                    textBorderWidth: 0,
                    color: "#FFF",
                    show: true,
                },
                // colorBy: "data",
                itemStyle: {
                    color: "rgba(255,255,255,1)",
                    borderColor: "rgba(2255,255,255,2)",
                    borderWidth: 4,
                    shadowColor: "#000",
                    shadowBlur: 10,
                },
            },
            // 动态生成业务分类散点图系列
            ...Object.values(BUSINESS_CATEGORIES).map(category => ({
                name: category.label,
                type: 'scatter',
                coordinateSystem: 'geo',
                symbol: 'image://' + category.icon,
                symbolSize: 25,
                itemStyle: {
                    color: category.color,
                    borderColor: '#fff',
                    borderWidth: 2,
                    shadowColor: category.color,
                    shadowBlur: 10
                },
                emphasis: {
                    symbolSize: 35,
                    itemStyle: {
                        shadowBlur: 15
                    }
                },
                label: {
                    show: true,
                    formatter: '{b}',
                    position: 'top',
                    color: '#fff',
                    fontSize: 11,
                    backgroundColor: 'rgba(0, 0, 0, 0.6)',
                    padding: [2, 6],
                    borderRadius: 3
                },
                data: categoryData.filter(item => item.category === category.key && item.visible).map(item => ({
                    name: item.name,
                    value: item.value
                }))
            }))
            
        ],
        //动画效果
        // animationDuration: 1000,
        // animationEasing: 'linear',
        // animationDurationUpdate: 1000
    };
}


export const regionCodes: any = {
    "中国": {
        "adcode": "100000",
        "level": "country",
        "name": "中华人民共和国"
    },
    "新疆维吾尔自治区": {
        "adcode": "650000",
        "level": "province",
        "name": "新疆维吾尔自治区"
    },
    "湖北省": {
        "adcode": "420000",
        "level": "province",
        "name": "湖北省"
    },
    "辽宁省": {
        "adcode": "210000",
        "level": "province",
        "name": "辽宁省"
    },
    "广东省": {
        "adcode": "440000",
        "level": "province",
        "name": "广东省"
    },
    "内蒙古自治区": {
        "adcode": "150000",
        "level": "province",
        "name": "内蒙古自治区"
    },
    "黑龙江省": {
        "adcode": "230000",
        "level": "province",
        "name": "黑龙江省"
    },
    "河南省": {
        "adcode": "410000",
        "level": "province",
        "name": "河南省"
    },
    "山东省": {
        "adcode": "370000",
        "level": "province",
        "name": "山东省"
    },
    "陕西省": {
        "adcode": "610000",
        "level": "province",
        "name": "陕西省"
    },
    "贵州省": {
        "adcode": "520000",
        "level": "province",
        "name": "贵州省"
    },
    "上海市": {
        "adcode": "310000",
        "level": "province",
        "name": "上海市"
    },
    "重庆市": {
        "adcode": "500000",
        "level": "province",
        "name": "重庆市"
    },
    "西藏自治区": {
        "adcode": "540000",
        "level": "province",
        "name": "西藏自治区"
    },
    "安徽省": {
        "adcode": "340000",
        "level": "province",
        "name": "安徽省"
    },
    "福建省": {
        "adcode": "350000",
        "level": "province",
        "name": "福建省"
    },
    "湖南省": {
        "adcode": "430000",
        "level": "province",
        "name": "湖南省"
    },
    "海南省": {
        "adcode": "460000",
        "level": "province",
        "name": "海南省"
    },
    "江苏省": {
        "adcode": "320000",
        "level": "province",
        "name": "江苏省"
    },
    "青海省": {
        "adcode": "630000",
        "level": "province",
        "name": "青海省"
    },
    "广西壮族自治区": {
        "adcode": "450000",
        "level": "province",
        "name": "广西壮族自治区"
    },
    "宁夏回族自治区": {
        "adcode": "640000",
        "level": "province",
        "name": "宁夏回族自治区"
    },
    "浙江省": {
        "adcode": "330000",
        "level": "province",
        "name": "浙江省"
    },
    "河北省": {
        "adcode": "130000",
        "level": "province",
        "name": "河北省"
    },
    "香港特别行政区": {
        "adcode": "810000",
        "level": "province",
        "name": "香港特别行政区"
    },
    "台湾省": {
        "adcode": "710000",
        "level": "province",
        "name": "台湾省"
    },
    "澳门特别行政区": {
        "adcode": "820000",
        "level": "province",
        "name": "澳门特别行政区"
    },
    "甘肃省": {
        "adcode": "620000",
        "level": "province",
        "name": "甘肃省"
    },
    "四川省": {
        "adcode": "510000",
        "level": "province",
        "name": "四川省"
    },
    "天津市": {
        "adcode": "120000",
        "level": "province",
        "name": "天津市"
    },
    "江西省": {
        "adcode": "360000",
        "level": "province",
        "name": "江西省"
    },
    "云南省": {
        "adcode": "530000",
        "level": "province",
        "name": "云南省"
    },
    "山西省": {
        "adcode": "140000",
        "level": "province",
        "name": "山西省"
    },
    "北京市": {
        "adcode": "110000",
        "level": "province",
        "name": "北京市"
    },
    "吉林省": {
        "adcode": "220000",
        "level": "province",
        "name": "吉林省"
    }
}

// 示例分类数据
export const sampleCategoryData: CategoryDataType[] = [
    
    {
        name: '庆云县出行点',
        category: 'shared_travel',
        value: [117.3698, 37.7825],
        visible: true
    }
];

// 庆云县专用分类数据
export const qingyunCategoryData: CategoryDataType[] = [
    {
        name: '绿电供应',
        category: 'green_power',
        value: [117.3856, 37.8452],
        visible: true
    },
    {
        name: '共享换电',
        category: 'shared_charging',
        value: [117.4412, 37.7658],
        visible: true
    },
    {
        name: '共享出行',
        category: 'shared_travel',
        value: [117.3698, 37.7825],
        visible: true
    },
    {
        name: '共享运力',
        category: 'shared_logistics',
        value: [117.3945, 37.7589],
        visible: true
    }
];

// 工具函数：根据分类过滤数据
export const filterDataByCategory = (data: CategoryDataType[], categories: string[]): CategoryDataType[] => {
    return data.filter(item => categories.includes(item.category) && item.visible);
};

// 工具函数：切换分类显示状态
export const toggleCategoryVisibility = (data: CategoryDataType[], category: string, visible: boolean): CategoryDataType[] => {
    return data.map(item => 
        item.category === category ? { ...item, visible } : item
    );
};

// 工具函数：获取分类统计信息
export const getCategoryStats = (data: CategoryDataType[]) => {
    const stats = Object.values(BUSINESS_CATEGORIES).reduce((acc, category) => {
        acc[category.key] = {
            total: data.filter(item => item.category === category.key).length,
            visible: data.filter(item => item.category === category.key && item.visible).length
        };
        return acc;
    }, {} as Record<string, { total: number; visible: number }>);
    
    return stats;
};

/**
 * 庆云县地图使用说明：
 * 
 * 1. 导入庆云县分类数据：
 *    import { qingyunCategoryData } from './center.map.ts';
 * 
 * 2. 在地图组件中使用：
 *    const option = optionHandle('qingyun', mapRegionData, effectScatterData, qingyunCategoryData);
 * 
 * 3. 自定义图标说明：
 *    - 所有分类标点使用统一的自定义图标：https://cdn-icons-png.flaticon.com/512/684/684908.png
 *    - 图标大小：25px（普通状态），35px（悬停状态）
 *    - 每个分类保持独特的颜色标识
 * 
 * 4. 庆云县标点坐标：
 *    - 绿电站：[117.3856, 37.7742]
 *    - 换电站：[117.4012, 37.7658]
 *    - 出行点：[117.3698, 37.7825]
 *    - 运力中心：[117.3945, 37.7589]
 * 
 * 5. 交互功能：
 *    - 点击图例可控制对应分类的显示/隐藏
 *    - 悬停标点可查看详细信息
 *    - 支持动态数据更新
 */